package com.zt.bigdata.elasticsearch.config;

import com.zt.bigdata.elasticsearch.utils.transport.ESClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.UnknownHostException;

/**
 *
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.zt.bigdata.elasticsearch.dao")
public class Config {
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;

    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;

    @Autowired
    private TransportClient transportClient;

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(transportClient);
    }

    @Bean
    public ESClient init() throws UnknownHostException {
        return new ESClient(clusterName, clusterNodes);
    }
}
